package cc.geguo.js.checkcode;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import org.apache.struts2.ServletActionContext;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

/**
 * 作者  Administrator
 * 时间  2017/3/16 17:54
 * 文件  Jquery41
 * 描述  用于处理用户传递过来的验证码
 * 1. 监听用户的输入
 * 2. ajax提交验证码
 * 3. 验证码输错
 * 4. 验证码正确
 * 5. 刷新验证码
 */
public class MyCheckAction extends ActionSupport{

	public String getCheckcode() {
		return checkcode;
	}

	public MyCheckAction() {
		System.out.println(this.getClass().getSimpleName() + ":MyCheckAction");
	}

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}

	private String checkcode;
	/**
	 * @return 发送验证码
	 * 1. 将参数和session中的数据进行比较
	 */
	public String checkRequest() throws IOException {
		String pic= "/images/MsgError.gif";

		String sessionCode = (String) ActionContext.getContext().getSession().get("CHECKNUM");
		if (checkcode.equals(sessionCode)){
			// 验证成功 返回图片地址(上级目录加/)
			pic="/images/MsgSent.gif";
		}

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset = utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(pic);
		System.out.println("pic"+pic);
		pw.flush();
		pw.close();
		return null;
	}
}
